{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ch06"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典 大括号\n",
    "alien_0={'color':'green','points':5}\n",
    "print(alien_0['color'])\n",
    "print(alien_0['points'])\n",
    "\n",
    "# 访问字典中的元素 中括号\n",
    "alien_0={'color':'green','points':5}\n",
    "new_points=alien_0['points']\n",
    "print(\"You just earned \"+str(new_points)+\" points!\")\n",
    "\n",
    "# 添加字典中的元素 中括号 快捷键ctrl+z\n",
    "\n",
    "alien_0={'color':'green','points':5}\n",
    "print(alien_0)\n",
    "alien_0['x_position']=0\n",
    "alien_0['y_position']=25\n",
    "print(alien_0)\n",
    "\n",
    "# 改变子弹中的元素 中括号\n",
    "\n",
    "alien_0={'color':'green','points':5}\n",
    "\n",
    "print(\"The alien is \"+alien_0['color']+'.')##当前颜色\n",
    "alien_0['color']='yellow'#改变颜色为黄色\n",
    "print(\"The alien is now \"+alien_0['color']+'.')\n",
    "\n",
    "# 对字典中的元素值做出判断\n",
    "\n",
    "alien_0={'x_position':0,\"y_position\":25,'speed':'medium'}\n",
    "print(\"Original x-position: \"+str(alien_0['x_position']))\n",
    "\n",
    "if alien_0['speed']=='slow':\n",
    "    x_increment=1\n",
    "elif alien_0['speed']=='medium':\n",
    "    x_increment=2\n",
    "else:\n",
    "    x_increment=3\n",
    "alien_0['x_position']=alien_0['x_position']+x_increment\n",
    "print(\"New x-position: \"+str(alien_0['x_position']))\n",
    "\n",
    "#删除\n",
    "alien_0={'color':'green','points':5}\n",
    "del alien_0['points']\n",
    "print(alien_0)\n",
    "\n",
    "#字典的其他格式，注意换行的位置，最后多加了一个逗号\n",
    "favorite_langues={\n",
    "    'jen':'python',\n",
    "    'sarah':'c',\n",
    "    'edward':'ruby',\n",
    "    'phil':'python',\n",
    "    }\n",
    "print(favorite_langues)\n",
    "\n",
    "#遍历字典 把前面这个单词，含义分别打出来\n",
    "user_0={\n",
    "    'username':'efermi',\n",
    "    'first':'enrico',\n",
    "    'last':'fermi',\n",
    "}\n",
    "\n",
    "for key, value in user_0.items():\n",
    "    print(\"\\nkey: \"+key)\n",
    "    print(\"Value: \"+value)\n",
    "\n",
    "# 生词 解释分开用\n",
    "favorite_langues={\n",
    "    'jen':'python',\n",
    "    'sarah':'c',\n",
    "    'edward':'ruby',\n",
    "    'phil':'python',\n",
    "    }\n",
    "\n",
    "for name, language in favorite_langues.items():\n",
    "    print(name.title()+\"'s favorite language is \"+language.title()+'.')\n",
    "\n",
    "for name in favorite_langues.keys():\n",
    "    print(name.title())\n",
    "\n",
    "# 对于字典中的人，打出名字，如果这个人在friends里面，多打出一句话\n",
    "favorite_langues={\n",
    "    'jen':'python',\n",
    "    'sarah':'c',\n",
    "    'edward':'ruby',\n",
    "    'phil':'python',\n",
    "    }\n",
    "\n",
    "friends=['phil','sarah']\n",
    "\n",
    "for name in favorite_langues.keys():\n",
    "    print(name.title())\n",
    "\n",
    "    if name in friends:\n",
    "        print(\"Hi, \"+name.title()+\", I see your favorite language is \"+favorite_langues[name].title()+\"!\")\n",
    "\n",
    "# 如果这个人不在喜欢语言这个名单中，说明他没参加采访，告诉他请投票\n",
    "favorite_langues = {\n",
    "    'jen': 'python',\n",
    "    'sarah': 'c',\n",
    "    'edward': 'ruby',\n",
    "    'phil': 'python',\n",
    "}\n",
    "\n",
    "if 'erin' not in favorite_langues.keys():\n",
    "    print(\"Erin, please take our poll!\")\n",
    "\n",
    "# 按顺序，字典中的东西没有顺序，我们如果想按一定的顺序打，需要用sorted\n",
    "favorite_langues = {\n",
    "    'jen': 'python',\n",
    "    'sarah': 'c',\n",
    "    'edward': 'ruby',\n",
    "    'phil': 'python',\n",
    "}\n",
    "# 按字母顺序打印出来这个人名\n",
    "for name in sorted(favorite_langues.keys()):\n",
    "        print(name.title()+\", thank you for taking the poll\")\n",
    "\n",
    "print(\"The following languages have been metioned:\")\n",
    "\n",
    "# 按原来字典中的顺序打印出语言，这里会重复打印\n",
    "for language in favorite_langues.values():\n",
    "    print(language.title())\n",
    "\n",
    "# 不重复打印，用set\n",
    "\n",
    "favorite_langues = {\n",
    "    'jen': 'python',\n",
    "    'sarah': 'c',\n",
    "    'edward': 'ruby',\n",
    "    'phil': 'python',\n",
    "}\n",
    "\n",
    "print(\"The following languages have been metioned:\")\n",
    "for language in set(favorite_langues.values()):\n",
    "    print(language.title())\n",
    "\n",
    "#嵌套  列表=['字典0','字典1','字典2']\n",
    "\n",
    "alien_0={'color':'green','points':5}\n",
    "alien_1={'color':'yellow','points':10}\n",
    "alien_2={'color':'red','points':15}\n",
    "\n",
    "aliens = [alien_0,alien_1,alien_2]\n",
    "for alien in aliens:\n",
    "    print(alien)\n",
    "\n",
    "# 生成30个外星人\n",
    "\n",
    "aliens=[]   ##创建一个空列表\n",
    "for alien_number in range(30): ##从0到29取值30次\n",
    "    new_alien={'color':'green','points':5,'speed':'slow'}\n",
    "    ##但这个外形人的编号在循环并没有使用\n",
    "    aliens.append(new_alien)\n",
    "\n",
    "for alien in aliens[:5]:\n",
    "    print(alien)##打印出前5个\n",
    "print('……')##后面的省略\n",
    "##打印出外星人的数量，用len，注意转换为str\n",
    "print(\"Total numeber of aliens: \"+str(len(aliens)))\n",
    "\n",
    "##前三个改成颜色速度分数值\n",
    "for alien in aliens[0:3]:\n",
    "    if alien['color']=='green':\n",
    "        alien['color']='yellow'\n",
    "        alien['speed']='medium'\n",
    "        alien['points']=10\n",
    "##打印出前五个\n",
    "for alien in aliens[:5]:\n",
    "    print(alien)\n",
    "print('……')\n",
    "\n",
    "## 前五个外星人升级\n",
    "for alien in aliens[0:5]:\n",
    "    if alien['color']=='green':\n",
    "        alien['color']='yellow'\n",
    "        alien['speed']='medium'\n",
    "        alien['points']=10\n",
    "    elif alien['color']=='yellow':\n",
    "        alien['color']='red'\n",
    "        alien['speed']='fast'\n",
    "        alien['points']=15\n",
    "\n",
    "for alien in aliens[:5]:\n",
    "    print(alien)\n",
    "print('……')\n",
    "\n",
    "\n",
    "# 字典={'单词1':'解释1','单词2':['解释1','解释2']}\n",
    "\n",
    "pizza={\n",
    "    'crust':'thick',\n",
    "    'toppings':['mushrooms','extra cheese'],\n",
    "}\n",
    "print('You ordered a '+pizza['crust']+'-crust pizza'+\" with the following toppings:\\n\")\n",
    "\n",
    "## 这里点名pizza字典里的crust这个词儿，把他的内容取出来过来\n",
    "\n",
    "for topping in pizza['toppings']:##这里吧toppings的值取出来\n",
    "    print('\\t-'+topping)\n",
    "\n",
    "\n",
    "# 字典中的多个列表\n",
    "\n",
    "favorite_langues={\n",
    "    'jen':['python','ruby'],\n",
    "    'sarah':['c'],\n",
    "    'edward':['ruby','go'],\n",
    "    'phil':['python','haskell'],\n",
    "}\n",
    "\n",
    "## 元素分开name取成值，languages取成列表\n",
    "\n",
    "for name,languages in favorite_langues.items():\n",
    "    print('\\n'+name.title()+\"'s favorite languages are:\")\n",
    "    ##从列表再提出元素\n",
    "    for language in languages:\n",
    "        print(\"\\t\"+language.title())\n",
    "\n",
    "# 字典中的字典 字典={字典0，字典1}\n",
    "# 字典={'新华字典':{'我':'美丽'}，'牛津字典':{'Me':'beau'}，'Le petit Nicola':{'Moi':'La plus belle'}}\n",
    "users={\n",
    "    'aeinstein':{\n",
    "        'first':'albert',\n",
    "        'last':'ainstein',\n",
    "        'location':'princeton',\n",
    "    },\n",
    "    'marie':{\n",
    "        'first':'marie',\n",
    "        'last':'curie',\n",
    "        'location':'paris',\n",
    "}\n",
    "\n",
    "for username, user_info in users.items():\n",
    "\n",
    "    print(\"\\nUsername: \"+username)\n",
    "\n",
    "    full_name=user_info['first']+\" \"+user_info['last']\n",
    "    location=user_info['location']\n",
    "\n",
    "    print('\\tFull name: '+full_name.title())\n",
    "    print('\\tLocation: '+location.title())\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 习题6-1 使用一个字典 存信息打出来\n",
    "\n",
    "durian_mille_crepe_cake={'durian':'炒了',\n",
    "                         'mille crepe':'煎了',\n",
    "                         'cake':'抹上',\n",
    "                         }\n",
    "\n",
    "print(durian_mille_crepe_cake)\n",
    "\n",
    "# 习题6-2\n",
    "\n",
    "favorite_number={'a':1,'b':2,'c':3,'d':4,'e':5}\n",
    "print(favorite_number)\n",
    "\n",
    "# 习题6-3\n",
    "\n",
    "python_words={'Code lay out':'代码布局',\n",
    "              'Whitespaces in Expressions':'表达式中的空格',\n",
    "              'Comments':'注释',\n",
    "              'Naming Conventions':'命名规范',\n",
    "              'programming recommendations':'编程建议'}\n",
    "\n",
    "# .items()不要忘，为了调用字典中的值\n",
    "\n",
    "for word, explaination in python_words.items():\n",
    "    print(\"\\n\"+word+\":\")\n",
    "    print(explaination)\n",
    "\n",
    "# 6-3 python专业词汇字典\n",
    "\n",
    "python_words={'Code lay out':'代码布局',\n",
    "              'Whitespaces in Expressions':'表达式中的空格',\n",
    "              'Comments':'注释',\n",
    "              'Naming Conventions':'命名规范',\n",
    "              'programming recommendations':'编程建议',\n",
    "              'Global Variable Names':'全局变量名',\n",
    "              'Function Annotation':'功能注释',\n",
    "              'Indentation':'缩进',\n",
    "              'Binary Operator':'运算符',\n",
    "              'String quotes':'字符串引号'\n",
    "              }\n",
    "\n",
    "# 习题6-4\n",
    "for word, explaination in python_words.items():\n",
    "    print(\"\\n\"+word+\":\")\n",
    "    print(explaination)\n",
    "\n",
    "# 6-5 河流 国家\n",
    "\n",
    "rivers={'La Seine':'France','yellow river':'China','Nile':'Egypt'}\n",
    "\n",
    "for river, country in rivers.items():\n",
    "    print(\"\\nThe \"+river.title()+\" runs through \"+country+'!!')\n",
    "\n",
    "for river, country in rivers.items():\n",
    "    print(river)\n",
    "\n",
    "for river, country in rivers.items():\n",
    "    print(country)\n",
    "\n",
    "# 6-6 名单列表，字典，在列表中不在列表中区别打印\n",
    "\n",
    "king_seven_armed_sea=['鹰眼','老沙','九蛇','明哥','甚平','月光莫利亚','熊','罗','巴基']\n",
    "the_defeated={'老沙':'歇菜','月光莫利亚':'完蛋','熊':'打不过跑','甚平':'收编'}\n",
    "\n",
    "for name in king_seven_armed_sea:\n",
    "    if name in the_defeated.keys():\n",
    "            print(name+'，嘿兄弟老熟人了啊')\n",
    "    else:\n",
    "        print(name,'，你等着')\n",
    "\n",
    "# 6-7 三个字典合成一个字典\n",
    "\n",
    "durian_mille_crepe_cake={'durian':'炒了',\n",
    "                         'mille crepe':'煎了',\n",
    "                         'cake':'抹上',\n",
    "                         }\n",
    "\n",
    "tomato_egg={'tomato':'切了',\n",
    "            'egg':'炒了',\n",
    "            'suger':'适量',\n",
    "            'salt':'适量'}\n",
    "\n",
    "palace_exploded_chicken_man={'peanut':'爆炒',\n",
    "                             'chicken':'腌好',\n",
    "                             'carrot':'切丁'}\n",
    "\n",
    "cook_books=[durian_mille_crepe_cake,\n",
    "            tomato_egg,\n",
    "            palace_exploded_chicken_man,\n",
    "            ]\n",
    "\n",
    "for dish in cook_books:\n",
    "    for ingredient, method in dish.items():\n",
    "        print(\"\\n材料：\"+ingredient+\"：\")\n",
    "        print(\"方法：\"+method)\n",
    "\n",
    "# 6-8 宠物\n",
    "\n",
    "tom={'type':'cat','host':'一个只有脚出境的女的'}\n",
    "jerry={'type':'耗子','host':'观众'}\n",
    "speike={'type':'dog','host':'一个只有脚出境的女的'}\n",
    "\n",
    "pets=[tom,jerry,speike]\n",
    "\n",
    "for pet in pets:\n",
    "    for key, value in pet.items():\n",
    "        print('\\n'+key+\":\"+value)\n",
    "\n",
    "# 6-9 喜欢的地方，字典套列表{'':[]}\n",
    "\n",
    "favorite_places={'依萍':['大上海','可云家','火车站'],\n",
    "                 '如萍':['书桓家','公园','上海大桥'],\n",
    "                 '陆飞':['如萍学校','报社','战场'],\n",
    "                 }\n",
    "\n",
    "for name, places in favorite_places.items():\n",
    "    print(\"\\n\"+name+'喜欢的地方：')\n",
    "    for place in places:\n",
    "        print(place)\n",
    "\n",
    "# 6-10 喜欢的数字\n",
    "favorite_number={'a':[1,2,3],\n",
    "                 'b':[4,5,6],\n",
    "                 'c':[7,8,9],\n",
    "                 'd':[10,11,12],\n",
    "                 'e':[13,14],\n",
    "                 }\n",
    "for name, numbers in favorite_number.items():\n",
    "    print(\"\\n\"+name+\"喜欢的数字是：\")\n",
    "    for number in numbers:\n",
    "        print(number)\n",
    "\n",
    "# 6-11 城市 字典套字典a={'a':{}}\n",
    "\n",
    "# bordeux={'country':'france',\n",
    "#          'population':243636,\n",
    "#          'fact':'水镜广场比特沙丘',\n",
    "#          }\n",
    "#\n",
    "# larochelle={'country':'france',\n",
    "#          'population':100000,\n",
    "#          'fact':'港口水族馆海贼梦世界尽头的灯塔',\n",
    "#          }\n",
    "#\n",
    "# barcelona={'country':'spain',\n",
    "#          'population':1610000,\n",
    "#          'fact':'建筑教堂佛朗明哥圆圈舞水果海鲜管够',\n",
    "#          }\n",
    "\n",
    "# places={bordeux,larochelle,barcelona}\n",
    "\n",
    "places={'bordeux':{\n",
    "         'country':'france',\n",
    "         'population':'243636',\n",
    "         'fact':'水镜广场比特沙丘',\n",
    "         },\n",
    "        'larochelle':{\n",
    "        'country':'france',\n",
    "         'population':'100000',\n",
    "         'fact':'港口水族馆海贼梦世界尽头的灯塔',\n",
    "         },\n",
    "        'barcelona':{'country':'spain',\n",
    "         'population':'1610000',\n",
    "         'fact':'建筑教堂佛朗明哥圆圈舞水果海鲜管够',\n",
    "         },\n",
    "        }\n",
    "\n",
    "for city, info in places.items():\n",
    "    print(\"\\n\\n\"+city+'介绍:')\n",
    "    for key, value in info.items():\n",
    "        print('\\n'+key+':'+value)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# 6-12 扩展，差不多行了哈，你以为我真的会去做吗，果断略过"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
